package com.briup.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.briup.comon.CustomException;
import com.briup.comon.R;
import com.briup.dto.SetmealDto;
import com.briup.entity.Dish;
import com.briup.entity.DishFlavor;
import com.briup.entity.Setmeal;
import com.briup.entity.SetmealDish;
import com.briup.mapper.SetmealMapper;
import com.briup.service.DishService;
import com.briup.service.SetmealDishService;
import com.briup.service.SetmealService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @Auther: liao
 * @Date: 2022/9/12-09-12-14:15
 * @Description：com.briup.service.impl
 */
@Service
public class SetmealServiceImpl extends ServiceImpl<SetmealMapper, Setmeal> implements SetmealService {
    /**
     * 新增套餐，同时需要保存套餐和菜品的关联关系
     * @param setmealDto
     */
    @Autowired
    private SetmealDishService setmealDishService;
    @Autowired
    private SetmealService setmealService;
    @Autowired 
    private DishService dishService;
    
    public void savewithDish(SetmealDto setmealDto) {
        //保存套餐的基本信息，操作setmeal，执行insert操作
        this.save(setmealDto);

        List<SetmealDish> setmealDishes = setmealDto.getSetmealDishes();
        setmealDishes.stream().map((item) -> {
            item.setSetmealId(setmealDto.getId());
            return item;
        }).collect(Collectors.toList());

        //保存套餐和菜品的关联信息，操作setmeal_dish,执行insert操作
        setmealDishService.saveBatch(setmealDishes);
        
    }

    /**
     * 更改套餐，同时需要更改套餐和菜品的关联关系
     * @param setmealDto
     */

    @Override
    public void updateWithDish(SetmealDto setmealDto) {

        //更改套餐的基本信息，操作setmeal，执行更新操作
        this.updateById(setmealDto);

        //清理当前菜品对应口味数据---dish_flavor表的delete操作
        LambdaQueryWrapper<SetmealDish> queryWrapper = new LambdaQueryWrapper();
        queryWrapper.eq(SetmealDish::getSetmealId,setmealDto.getId());

        setmealDishService.remove(queryWrapper);

        List<SetmealDish> setmealDishes = setmealDto.getSetmealDishes();
        setmealDishes.stream().map((item) -> {
            item.setSetmealId(setmealDto.getId());
            return item;
        }).collect(Collectors.toList());

        //更改套餐和菜品的关联信息，操作setmeal_dish,执行insert操作
        setmealDishService.saveBatch(setmealDishes);
    }

    @Override
    public void delete(List<Long> ids) {
        //删除的话  要删除两个表  一个是套餐表  一个是套餐和菜品表 里面都是有菜品的东西
        
        //先删除套餐表
        //先查询这些id的套餐的售卖状态
        List<Setmeal> setmeals = setmealService.listByIds(ids);
        if (setmeals!=null){
            //且要判断这个套餐是否在售，在售的话，不能删除
            for (Setmeal setmeal : setmeals) {
                if (setmeal.getStatus()==0){
                    this.removeById(setmeal.getId());
                }else {
                    throw new CustomException("商品在售中，不能删除哦");
                }
            }
        }


        //通过套餐的id可以删除那些关联的菜品
        LambdaQueryWrapper<SetmealDish> queryWrapper = new LambdaQueryWrapper();
        queryWrapper.in(SetmealDish::getSetmealId,ids);
        setmealDishService.remove(queryWrapper);
        
     
        
    }


}
